package com.commerceerp.system.job;

import com.commerceerp.system.entity.ForderInfo;
import com.commerceerp.system.entity.OrderFinishRecord;
import com.commerceerp.system.entity.PorderInfo;
import com.commerceerp.system.mapper.ForderMapper;
import com.commerceerp.system.mapper.OFRecordMapper;
import com.commerceerp.system.mapper.OrderMapper;
import com.commerceerp.system.mapper.PorderMapper;
import com.commerceerp.system.service.FactoryService;
import com.commerceerp.system.service.PurchaseOrderService;
import com.commerceerp.system.util.CommonUtil;
import com.commerceerp.system.util.DateUtil;
import com.commerceerp.system.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component
public class AddAdvicePorderJob {

    @Autowired
    private FactoryService factoryService;

    @Autowired
    private PorderMapper porderMapper;

    @Autowired
    private PurchaseOrderService porderService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OFRecordMapper ofRecordMapper;

    @Autowired
    private ForderMapper forderMapper;

    @Scheduled(cron="0 0/60 * * * ?") //每60分钟执行一次
    public void AddAdvicePorder() {

        List<Map> list = factoryService.getAllGoodStorehouseInfo(null,null,null,null,null,null,null,null);

        for(Map<String,Object> map : list){

            Integer gsSafeNumber = Integer.parseInt(map.get("gsSafeNumber").toString());
            Integer gsResidueNumber = Integer.parseInt(map.get("gsResidueNumber").toString());
            String goodId = map.get("gsGoodId").toString();
            String gsStorehouseId = map.get("gsStorehouseId").toString();

            if(gsResidueNumber <= gsSafeNumber){
                Integer advicePorderNum = porderMapper.getAdvicePorderByGoodId(goodId);
                if(advicePorderNum < 1){

                    Integer numInRoad = this.porderMapper.getCountByDiffPorderStatus("4",goodId.toString());

                    PorderInfo porderInfo = new PorderInfo();
                    porderInfo.setPorderStatus("1");
                    porderInfo.setPorderId(StringUtils.getUUID());
                    porderInfo.setPorderStorehouseId(gsStorehouseId);
                    porderInfo.setPorderGoodId(goodId);
                    porderInfo.setPorderArriveNumber(numInRoad);
                    porderService.saveNewPorderInfo(porderInfo);

                }

            }
        }

    }



    //@Scheduled(cron="0 0/360 * * * ?") //每60分钟执行一次
    public void getAllOrderToAddOFRecord() {


        List<Map> orderList = this.orderMapper.getAllGoodInfos();

        for(Map<String,Object> orderMap : orderList){

            if(orderMap != null){

                Double orderMoney = Double.valueOf(CommonUtil.getIntByString(orderMap.get("orderMoney") == null? "0.0" : orderMap.get("orderMoney").toString()));
                Double orderPrice = Double.valueOf(CommonUtil.getIntByString(orderMap.get("orderGoodPrice") == null ? "0.0" :orderMap.get("orderGoodPrice").toString()));
                Double orderProfit = (orderMoney-orderPrice)*Double.valueOf(orderMap.get("orderMoney") == null? "0" : orderMap.get("orderGoodNumber").toString());

                OrderFinishRecord orderFinishRecord = new OrderFinishRecord();

                if(orderMap != null){

                    orderFinishRecord.setOfrProfit(new BigDecimal(orderProfit.toString()).setScale(2,BigDecimal.ROUND_HALF_UP).toString());
                    orderFinishRecord.setOfrAmount(orderMap.get("orderGoodNumber") == null ? "0": orderMap.get("orderGoodNumber").toString());
                    orderFinishRecord.setOfrCreateTime(DateUtil.getYMDHMSDate());
                    orderFinishRecord.setOfrGoodCost(CommonUtil.getIntByString(orderMap.get("orderMoney") == null? "0.0" : orderMap.get("orderMoney").toString()));
                    orderFinishRecord.setOfrGoodId(orderMap.get("orderSku") == null ? null: orderMap.get("orderSku").toString());
                    orderFinishRecord.setOfrOrderId(orderMap.get("orderId") == null ? null : orderMap.get("orderId").toString());

                    ofRecordMapper.saveNewOFRecord(orderFinishRecord);
                }
            }

        }
    }


    //@Scheduled(cron="0 0/360 * * * ?") //每60分钟执行一次
    public void getForderInfoByOrder() {

        List<Map> orderList = this.orderMapper.getAllGoodInfos();

        for(Map<String,Object> orderMap : orderList){

            if(orderMap != null){
                ForderInfo forderInfo = new ForderInfo();
                String strID = UUID.randomUUID().toString().substring(0,10);
                forderInfo.setForderId(strID);
                forderInfo.setForderMoney(new BigDecimal(CommonUtil.getIntByString(orderMap.get("orderMoney") == null? "0" : orderMap.get("orderMoney").toString())));
                forderInfo.setForderPorderId(orderMap.get("orderId") == null? null : orderMap.get("orderId").toString());
                forderInfo.setForderType(null);
                forderInfo.setForderCreateTime(DateUtil.getYMDHMSDate());
                forderInfo.setForderStatus("4");
                forderInfo.setForderPayTime(orderMap.get("orderPayTime") == null? null : orderMap.get("orderPayTime").toString());
                forderInfo.setForderInOut("1");
                forderInfo.setForderReceivablesTime(orderMap.get("orderPayTime") == null? null : orderMap.get("orderPayTime").toString());

                this.forderMapper.saveNewForderInfoByJob(forderInfo);

                this.forderMapper.saveNewForderRecordByJob(strID,orderMap.get("orderId") == null? null : orderMap.get("orderId").toString(),"2",new BigDecimal(orderMap.get("orderMoney") == null? "0" : CommonUtil.getIntByString(orderMap.get("orderMoney").toString())),DateUtil.getYMDHMSDate(),"订单收入");
            }
        }
    }

}
